<template>
  <view class="cate-container">
    <my-search @searchClick="gotoSearch"></my-search>
    <!-- 分类列表 -->
    <view class="cate-scroll-box">
      <!-- 左侧滚动栏 -->
      <scroll-view scroll-y :style="{height: windowHeight + 'px'}" class="left-scroll-box">
        <block v-for="(item, index) in categoriesList">
          <view :class="index === actived ? 'actived' : ''" @click="activeChange(index)">
            {{ item.cat_name }}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧滚动栏 -->
      <scroll-view scroll-y :style="{height: windowHeight + 'px'}" class="right-scroll-box" :scroll-top="scrollTop">
        <view class="leve2-list" v-for="(item, index) in leveList2" :key="index">
          <!-- 右侧二级菜单栏标题 -->
          <view class="leve2-title">
            / {{ item.cat_name }} /
          </view>
          <!-- 右侧二级菜单栏列表 -->
          <view class="leve3-list">
            <view class="leve3-list-item" v-for="(item3, index) in item.children" @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon" class="leve3-image"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>
Cate
<script>
  // 导入badgeMixins文件
  import badgeMixins from '../../mixins/badgeMixins.js'
  export default {
    mixins: [badgeMixins],
    data() {
      return {
        // 当前设备可使用窗口的高度
        windowHeight: 0,
        // 分类页面数据
        categoriesList: [],
        // 左侧滚动栏当前激活项
        actived: 0,
        // 二级菜单数据
        leveList2: [],
        // 滚动条距离页面顶部的距离
        scrollTop: 0
      };
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('api/public/v1/categories')
        console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.categoriesList = res.message
        // 二级菜单数据
        this.leveList2 = res.message[0].children
        console.log(this.leveList2)
      },
      // 改变 active 
      activeChange(index) {
        // 为 actived 重新赋值
        this.actived = index
        // 为 leveList2 重新赋值
        this.leveList2 = this.categoriesList[index].children
        // 为 scrollTop重新赋值
        this.scrollTop = this.scrollTop === Math.random() ? Math.random() + 0.1 : Math.random()
      },
      // 跳转商品列表页面
      gotoGoodsList(item3) {
        console.log(item3)
        uni.navigateTo({
          url: '/subPackages/goods_list/goods_list?cid=' + item3.cat_id
        })
      },
      // 跳转到搜索页面
      gotoSearch() {
        uni.navigateTo({
          url: '/subPackages/search/search'
        })
      }
    },
    onLoad() {
      // 获取当前设备的可用窗口高度
      const wh = uni.getSystemInfoSync().windowHeight
      this.windowHeight = wh - 50
      // 获取分类页面数据
      this.getCateList()
    }
  }
</script>

<style lang="scss">
  .cate-container {
    .cate-scroll-box {
      display: flex;

      .left-scroll-box {
        width: 240rpx;
        line-height: 100rpx;
        text-align: center;
        font-size: 12px;
        background-color: #F7F7F7;

        .actived {
          position: relative;
          background-color: #fff;

          &::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            width: 6rpx;
            height: 50rpx;
            transform: translateY(-50%);
            background-color: #ff3f34;
            border-radius: 6rpx;
          }
        }
      }

      .right-scroll-box {
        .leve2-list {
          .leve2-title {
            text-align: center;
            padding: 20rpx 0;
            color: black;
            font-weight: 700;
            font-size: 24rpx;
          }
          .leve3-list {
            display: flex;
            flex-wrap: wrap;
            .leve3-list-item {
              display: flex;
              flex-direction: column;
              justify-content: center;
              align-items: center;
              font-size: 24rpx;
              margin-bottom: 20rpx;
              width: 33.33%;
              .leve3-image {
                width: 120rpx;
                height: 120rpx;
              }
            }
          }
        }
      }
    }
  }
</style>
